package com.dw.article.controller;



import com.dw.article.service.ArticleUserService;
import com.dw.article.service.CategoryInfoService;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.utils.ResultUtils;
import com.dw.model.article.dtos.category.AddCategoryDTO;
import com.dw.model.article.dtos.category.AddCategoryHotDTO;
import com.dw.model.article.dtos.category.QueryCategoryDTO;
import com.dw.model.article.entitys.CategoryInfo;
import com.dw.model.article.vos.category.QueryCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文章分类表(CategoryInfo)表控制层
 *
 * @author makejava
 * @since 2024-10-05 19:44:34
 */
@Slf4j
@RestController
@RequestMapping("/article/category")
public class CategoryInfoController {

    @Resource
    private CategoryInfoService categoryInfoService;

    @Resource
    private ArticleUserService articleUserService;

    /**
     * 新增分类
     */
    @PostMapping("/add")
    private BaseResponse<Boolean> addCategory(@RequestBody AddCategoryDTO addCategoryDTO){
        log.info("新增分类参数:{}", addCategoryDTO);
        Boolean result = categoryInfoService.addCategory(addCategoryDTO);
        return ResultUtils.success(result);
    }

    /**
     * 条件分页查询所有分类
     */
    @PostMapping("/query")
    private BaseResponse<List<QueryCategoryVO>> queryCategory(@RequestBody QueryCategoryDTO queryCategoryDTO){
        log.info("条件分页查询所有分类参数:{}",queryCategoryDTO);
        List<CategoryInfo> list = categoryInfoService.queryCategory(queryCategoryDTO);
        // 类型转换
        List<QueryCategoryVO> collect = list.stream().map(item -> {
            QueryCategoryVO queryCategoryVO = new QueryCategoryVO();
            queryCategoryVO.setLeiId(String.valueOf(item.getLeiId()));
            queryCategoryVO.setCategoryName(item.getCategoryName());
            return queryCategoryVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

    /**
     * 根据分类id列表查询分类信息
     */
    @PostMapping("/queryByIds")
    private BaseResponse<List<QueryCategoryVO>> queryCategoryByIds(@RequestBody QueryCategoryDTO queryCategoryDTO){
        log.info("根据分类id列表查询分类信息:{}",queryCategoryDTO.getCategoryIds());
        List<CategoryInfo> list = categoryInfoService.queryCategoryByIds(queryCategoryDTO.getCategoryIds());
        // 类型转换
        List<QueryCategoryVO> collect = list.stream().filter(Objects::nonNull).map(item -> {
            QueryCategoryVO queryCategoryVO = new QueryCategoryVO();
            BeanUtils.copyProperties(item, queryCategoryVO);
            return queryCategoryVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

    /**
     * 查询热门分类
     */
    @GetMapping("/queryHot")
    private BaseResponse<List<QueryCategoryVO>> queryHotCategory(){
        log.info("查询热门分类");
        List<CategoryInfo> list = articleUserService.queryHotCategory();
        // 类型转换
        List<QueryCategoryVO> collect = list.stream().filter(Objects::nonNull).map(item -> {
            QueryCategoryVO queryCategoryVO = new QueryCategoryVO();
            queryCategoryVO.setLeiId(String.valueOf(item.getLeiId()));
            queryCategoryVO.setCategoryName(item.getCategoryName());
            queryCategoryVO.setCategoryFire(item.getCategoryFire());
            return queryCategoryVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }
}

